<?php
// 提交添加医生信息
// header("content-type:text/html;charset=UTF-8");
include_once("conn.php");

$title = $_POST["title"];
$content = $_POST["content"];

$pic = $_FILES['pic'];

if (!in_array($pic['type'], ['image/jpg', 'image/jpeg', 'image/png'])) {
    die("<script>alert('图片格式错误！请重新选择！');history.go(-1);</script>");
}

if ($pic['size'] > 2000000) {
    die("<script>alert('图片大小错误！大小不能超过2M！请重新选择！');history.go(-1);</script>");
}
date_default_timezone_set("Asia/Shanghai");
$addDate = date("Y-m-d");
$addTime = date("H:i:s");
// 图片上传路径
$uploadAddress = "../public/pic/carousel/";
// 图片名称
$length = 10;
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$random_string = '';

for ($i = 0; $i < $length; $i++) {
    $random_char = $characters[mt_rand(0, strlen($characters) - 1)];
    $random_string .= $random_char;
}

$picName = $random_string . "." . pathinfo($pic['name'], PATHINFO_EXTENSION);
// 完整路径
$upload = $uploadAddress . $picName;

if (move_uploaded_file($pic['tmp_name'], $upload)) {
    $insert_addinfo = "INSERT INTO carousel_pic_infos (pic_Name, pic_Title, pic_Content, pic_AddDate, pic_AddTime)
    VALUES ('" . $picName . "', '" . $title . "', '" . $content . "', '" . $addDate . "', '" . $addTime . "')";
    if (mysqli_query($connect_DB, $insert_addinfo)) {
        echo "<script>alert('添加轮播图信息成功！');location='admin_ControlIndexPic.php';</script>";
    } else {
        unlink($upload);
        die("<script>alert('添加轮播图信息失败！请重试！');</script>");
    }
} else {
    die("<script>alert('轮播图上传失败！请重试！');history.go(-1);</script>");
}

$connect_DB->close();
?>